/// <license>
/// This software and are provided as-is and without  warranty. 
/// This is  free  software,  as long  as you  clearly  display
/// the original author and his company for at least  1  second
/// on the attached display. If you need a commercial   license 
/// in which you do not have these restrictions, please contact
/// us via our website.
/// 
/// Author: Mario Di Vece
/// Unosquare S.A. de C.V. Copyright (c) 2013
/// url: http://www.unosquare.com
/// </license>

namespace Unosquare.Labs.Drivers.Vizic
{
    using Unosquare.Labs.Utils;
 
    /// <summary>
    /// Represents a Touch Response Type and its touch point position
    /// </summary>
    public class VizicTouchResponse
    {
        /// <summary>
        /// Creates a No-Touch default response
        /// </summary>
        /// <value>
        /// The no touch.
        /// </value>
        static public VizicTouchResponse NoTouch
        {
            get
            {
                return new VizicTouchResponse();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VizicTouchResponse"/> class as a No-Touch response type.
        /// </summary>
        public VizicTouchResponse()
        {
            this.X = 0;
            this.Y = 0;
            this.ResponseType = VizicTouchResponseTypes.NoTouchData;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VizicTouchResponse"/> class from a byte array.
        /// </summary>
        /// <param name="r">The r.</param>
        public VizicTouchResponse(byte[] r)
            : this()
        {

            // No touch
            if (r[0] == 0x4e && r[1] == 0x4f && r[2] == 0x4e && r[3] == 0x45)
            {
                this.ResponseType = VizicTouchResponseTypes.NoTouchData;
                return;
            }

            // HOME
            if (r[0] == 0x48 && r[1] == 0x4F && r[2] == 0x4D && r[3] == 0x45)
            {
                this.ResponseType = VizicTouchResponseTypes.HomeIcon;
                return;
            }

            // BOOK
            if (r[0] == 0x42 && r[1] == 0x4F && r[2] == 0x4F && r[3] == 0x4B)
            {
                this.ResponseType = VizicTouchResponseTypes.BookIcon;
                return;
            }

            // MESG
            if (r[0] == 0x4d && r[1] == 0x45 && r[2] == 0x53 && r[3] == 0x47)
            {
                this.ResponseType = VizicTouchResponseTypes.MessageIcon;
                return;
            }

            // TELE
            if (r[0] == 0x54 && r[1] == 0x45 && r[2] == 0x4c && r[3] == 0x45)
            {
                this.ResponseType = VizicTouchResponseTypes.TelephoneIcon;
                return;
            }

            // SONG
            if (r[0] == 0x53 && r[1] == 0x4f && r[2] == 0x4e && r[3] == 0x47)
            {
                this.ResponseType = VizicTouchResponseTypes.SongIcon;
                return;
            }

            this.ResponseType = VizicTouchResponseTypes.PositionData;
            this.X = r.Slice(0, 2).ToUInt16();
            this.Y = r.Slice(2, 2).ToUInt16();

        }

        /// <summary>
        /// Gets or sets the type of the response.
        /// </summary>
        /// <value>
        /// The type of the response.
        /// </value>
        public VizicTouchResponseTypes ResponseType { get; set; }

        /// <summary>
        /// The X coordinate of the touch point.
        /// </summary>
        /// <value>
        /// The X.
        /// </value>
        public ushort X { get; set; }

        /// <summary>
        /// Gets or sets the Y coordinate of the touch point.
        /// </summary>
        /// <value>
        /// The Y.
        /// </value>
        public ushort Y { get; set; }
    }

}
